package TcpUdpDemp;

import javax.xml.crypto.Data;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {
        //1.创建客户端对象
        DatagramSocket socket = new DatagramSocket(6665);

//         public DatagramPacket(byte buf[], int length,
//        InetAddress address, int port) {
        //参数一：封装要发出去的数据
        //参数二：发送出去的数据大小（字节个数）
        //参数三：服务端的ip地址（服务端主机）
        Scanner sc = new Scanner(System.in);
        while (true) {
            //2.创建数据包对象，封装要发出去的数据
            System.out.println("[Msg]");
            String msg = sc.next();
            if ("exit".equals(msg)) {
                System.out.println("成功退出，欢迎下次使用！");
                socket.close();
            }
            byte[] bytes = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes,bytes.length,
                    InetAddress.getLocalHost(),6666);
            //3.正式开始发送数据包出去
            socket.send(packet);
        }
    }
}
